<template>
  <div>
    <van-button
      style="position: fixed; top: 30px; right: 0; z-index: 999"
      type="primary"
      >loading:{{ loading }}===finished:{{ finished }}refreshing:
      {{ refreshing }}=={{ list.length }}</van-button
    >
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="100"
      >
        <van-cell
          v-for="(item, index) in list"
          :key="index"
          :title="index + '===' + item.goods_name"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data () {
    return {
      list: [],
      loading: false,//加载状态,只要onLoad触发就会变成true
      finished: false,//到底了就变成了true,结束上啦
      pagenum: 1,
      pagesize: 20,
      refreshing: false
    };
  },
  methods: {
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        // for (let i = 0; i < 10; i++) {
        //   this.list.push(this.list.length + 1);
        // }

        // // 加载状态结束
        // this.loading = false;

        // // 数据全部加载完成
        // if (this.list.length >= 40) {
        //   this.finished = true;
        // }
        if (this.pagenum > 3) {
          this.loading = false

          this.finished = true
          return
        }
        this.getData()
        this.pagenum += 1


      }, 1000);
    },
    async getData () {
      const res = await axios({
        url: 'http://vue-shop-api-t.itheima.net/api/private/v1/goods',
        headers: {
          Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2ODU0MzE3ODcsImV4cCI6MTY4NTUxODE4N30.C5Yb70H6HNeahpTFpxecbOx7YU4AnhzN0fyXieMG-Fk'
        },
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize,

        }
      })
      console.log(res)
      this.list = this.refreshing ? res.data.data.goods : [...this.list, ...res.data.data.goods]
      this.loading = false//结束当前上拉

      this.refreshing = false//关闭下拉
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false;  // finished:true 不能开启上啦 表示又可以继续上啦

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      // this.loading = true;
      this.pagenum = 1
      this.onLoad();
    },
  },
};
</script>
<style lang='less'  scoped>
</style>
